<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\WorksUpload;

/**
 * Class Report
 * @package App\Models
 */
class Report extends Model
{
    protected $table = 'xyj_reports';

    /**
     * 举报人
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function user()
    {
        return $this->belongsTo('\App\Models\User', 'user_id');
    }

    /**
     * 被举报作品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function works()
    {
        return $this->belongsTo('\App\Models\Works', 'works_id');
    }

    public function files()
    {
        return $this->hasMany('\App\Models\File', 'report_id');
    }

    public function worksType()
    {
        return $this->belongsTo('\App\Models\WorksType', 'type_id');
    }

    /**
     * 附件
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function worksUploads()
    {
        return $this->hasMany('\App\Models\WorksUpload', 'works_id' , 'works_id');
    }

    public function getUploads()
    {
        $uploads = [];
        $fengmian = WorksUpload::where(['works_id' => $this->works_id , 'type' => '封面'])->first();
        if ($fengmian){
            $uploads[] = $fengmian;
        }
        $mulus = WorksUpload::where(['works_id' => $this->works_id , 'type' => '目录'])->orderBy('sort' , 'asc')->get();
        if ($mulus){
            foreach ($mulus as $mulu){
                $uploads[] = $mulu;
            }
        }
        $neirongs = WorksUpload::where(['works_id' => $this->works_id , 'type' => '内容'])->orderBy('sort' , 'asc')->get();
        if ($neirongs){
            foreach ($neirongs as $neirong){
                $uploads[] = $neirong;
            }
        }
        $fengdi = WorksUpload::where(['works_id' => $this->works_id , 'type' => '封底'])->first();
        if ($fengdi){
            $uploads[] = $fengdi;
        }
        return $uploads;
    }

    public function worksTexts()
    {
        return $this->hasOne('\App\Models\WorksText', 'works_id' , 'works_id');
    }
}